package com.example.bestudentsystem.demos.web.controller;

import com.example.bestudentsystem.demos.web.mapper.SettingsMapper;
import com.example.bestudentsystem.demos.web.pojo.Settings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
public class SettingsController {

    @Autowired
    private SettingsMapper settingsMapper;

    /**
     * 获取成绩录入状态
     */
    @GetMapping("/scoreEntryStatus")
    public Map<String, Object> getScoreEntryStatus() {
        // 查询 "scoreEntryEnabled" 配置项
        Settings setting = settingsMapper.selectById(1); // 假设 id 为 1 表示该项配置
        Map<String, Object> response = new HashMap<>();
        response.put("enabled", setting != null && "true".equals(setting.getValue()));
        return response;
    }

    /**
     * 切换成绩录入状态
     */
    @PostMapping("/toggleScoreEntry")
    public Map<String, String> toggleScoreEntry() {
        // 查询 "scoreEntryEnabled" 配置项
        Settings setting = settingsMapper.selectById(1);

        if (setting == null) {
            // 如果配置项不存在，初始化为禁用状态
            setting = new Settings(1, "scoreEntryEnabled", "true");
            settingsMapper.insert(setting);
        } else {
            // 切换状态
            setting.setValue("true".equals(setting.getValue()) ? "false" : "true");
            settingsMapper.updateById(setting);
        }

        Map<String, String> response = new HashMap<>();
        response.put("message", "成绩录入状态切换成功！");
        response.put("newStatus", setting.getValue());
        return response;
    }
}
